package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.excel.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ModuleService moduleService;

    //查询
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "5")int size){

        PageInfo info = roleService.findAll(page, size, getLoginCompanyId());

        request.setAttribute("page",info);

        return "system/role/role-list";
    }

    //进入添加
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "system/role/role-add";
    }
    //添加
    @RequestMapping("/edit")
    public String edit(Role role){
        role.setCompanyId(getLoginCompanyId());
        role.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(role.getId())) {
            roleService.save(role);
        }else {
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";
    }

    //进入添加
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        Role role = roleService.findById(id);
        request.setAttribute("role",role);
        return "system/role/role-update";
    }
    //删除
    @RequestMapping("/delete")
    public String delete (String id){
        roleService.delete(id);
        return "redirect:/system/role/list.do";
    }
    //roleModule.do?roleid=4028a1c34ec2e5c8014ec2ebf8430001

    //进入权限分配
    @RequestMapping("/roleModule")
    public String roleModule(String roleid){
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);
        return "system/role/role-module";
    }

    //加载z树
    @RequestMapping("/getZtreeNodes")
    public @ResponseBody List<Map>
    getZtreeNodes(String roleId){
        List<Map> list = new ArrayList<>();
        List<Module> modules = moduleService.findAll();
        List<String> moduleIds = roleService.findRoleModule(roleId);
        for (Module module : modules) {
            HashMap map = new HashMap();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());
            if (moduleIds.contains(module.getId())){
                map.put("checked",true);
            }
            list.add(map);
        }
        return list;
    }

    //保存角色分配
    @RequestMapping("/updateRoleModule")
    public String updateRoleModule(String roleid,String moduleIds){

        roleService.saveRoleModule(roleid,moduleIds);
        return "redirect:/system/role/list.do";
    }



}
